<template>
	<view class="page-container">
		<view class="register">
			<view class="register-title">欢迎加入！</view>
			<view class="register-phone">
				<u-input type="number" clearable placeholder="请输入手机号码" v-model="registerForm.phone" shape="circle"
					prefixIcon="account" />
			</view>
			<view class="register-code">
				<u-input type="number" placeholder="6位数验证码" v-model="registerForm.verifyCode" shape="circle">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="x秒重新获取"></u-code>
						<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
					</template>
				</u-input>
			</view>
			<view class="register-password">
				<u-input type="password" clearable placeholder="请输入8-18位含有数字和字母的密码" v-model="registerForm.password"
					shape="circle" prefixIcon="lock" />
			</view>
			<view class="register-btn">
				<u-button type="primary" shape="circle" @click="handlerRegister">注册</u-button>
			</view>
			<view class="register-login">
				已有账号，<text style="color: #007AFF;" @click="handleGoLogin">马上登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		checkPassword,
		checkTel
	} from '@/utils/index.js'
	import {
		post,
		get
	} from '@/utils/http.js'
	export default {
		data() {
			return {
				tips: '获取验证码',
				registerForm: {
					phone: '',
					verifyCode: '',
					password: ''
				}
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			/**
			 * 获取验证码
			 */
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						if (!checkTel(this.registerForm.phone)) {
							uni.showToast({
								title: '手机号码不正确',
								icon: 'none'
							})
							return
						}

						get('user', 'sendVerifyCode', {
							phone: this.registerForm.phone
						}).then(res => {
							console.log(res)
							const {
								data,
								errmsg,
								errno
							} = res
							if (200 === errno) {
								this.registerForm.verifyCode = data
							} else {
								uni.showToast({
									title: '获取验证码失败',
									icon: 'none'
								})
							}
						}).catch(err => {
							uni.showToast({
								title: err.errmsg,
								icon: 'none'
							})
						})
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			/**
			 * 注册
			 */
			handlerRegister() {
				if (!checkPassword(this.registerForm.password)) {
					uni.showToast({
						title: '请输入8-18位含有数字和字母的密码'
					})
					return
				}
				if (!checkTel(this.registerForm.phone)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						icon: 'none'
					})
					return
				}
				if (this.registerForm.verifyCode.length !== 6) {
					uni.showToast({
						title: '验证码应为6位数',
						icon: 'none'
					})
					return
				}
				get('user', 'register', this.registerForm).then(res => {
					const {
						data,
						errmsg,
						errno
					} = res
					if (200 === errno) {
						uni.showToast({
							title: '注册成功',
							icon: 'none'
						})
						uni.navigateTo({
							url: '/pages/public/login'
						})
					} else {
						uni.showToast({
							title: errmsg,
							icon: 'none'
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err.errmsg,
						icon: 'none'
					})
				})
			},
			handleGoLogin() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	uni-page-body,
	.page-container {
		width: 100%;
		height: 100%;
	}

	.page-container {
		background-color: #017F7C;
		display: flex;
		justify-content: center;
		align-items: center;

		.register {
			width: 80%;
			height: 70%;
			background-color: white;
			border-radius: 20rpx;
			padding: 40rpx 40rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;

			.register-title {
				font-size: 50rpx;
				text-align: center;
				// padding: 50rpx ;
			}

			.register-login {
				text-align: center;
			}
		}
	}
</style>
